package me.atbar.moonlib.socreboard;

import me.atbar.moonlib.util.ColorUtils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Scoreboard;

public class MoonBoard {

    private final Scoreboard board;
    private final Objective objective;

    public MoonBoard(String displayName, String name) {
        board = createBoard();
        objective = board.registerNewObjective(ColorUtils.color(name), "dummy", ColorUtils.color(displayName));
        objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    }

    private Scoreboard createBoard() {
        return Bukkit.getServer().getScoreboardManager().getNewScoreboard();
    }

    public void addContent(String text, Integer line) {
        objective.getScore(text).setScore(line);
    }

    public void addContent(BoardContent content) {
        content.getContentMap().keySet().forEach((score) -> {
            objective.getScore(content.getContent(score)).setScore(score);
        });
    }

    public void addPlayer(Player player) {
        player.setScoreboard(board);
    }

    public void addAllPlayer() {
        Bukkit.getServer().getOnlinePlayers().forEach((player) -> {
            player.setScoreboard(board);
        });
    }

    public boolean update() {
        return true;
    }

}
